package com.intech.android.demo.cms.beans;


import com.intech.android.demo.cms.bo.Category;
import com.intech.android.demo.cms.common.Constants;
import com.intech.android.demo.cms.common.FacesHelper;
import com.intech.android.demo.cms.common.Util;
import com.intech.android.demo.cms.decorators.CategoryDecorator;

import java.util.ArrayList;
import java.util.List;

public class CategoryBean extends BaseBean
{
    private String catId;
    private String name;
    private String description;
    List<CategoryDecorator> categoryDecorators;
    List<Category> categoryBOs;
    private String imageSrc;

    public String editCategory()
    {
        Integer category_id = FacesHelper.getParameterAsInteger("edit_category");
        if (category_id == null)
        {
            return Constants.Navigation.CATEGORIES_LINK;
        }
        fillAddRegionForm(category_id);
        return Constants.Navigation.ADD_CATEGORY_LINK;
    }

    public String addCategory()
    {
        catId = null;
        imageSrc = null;
        name = null;
        description = null;
        return Constants.Navigation.ADD_CATEGORY_LINK;
    }

    private void fillAddRegionForm(Integer categoryNumber)
    {
        CategoryDecorator currentDecorator = new CategoryDecorator();
        for (CategoryDecorator categoryDecorator : categoryDecorators)
        {
            if (Integer.parseInt(categoryDecorator.getId()) == categoryNumber)
            {
                currentDecorator = categoryDecorator;
            }
        }

        catId = currentDecorator.getId();
        imageSrc = currentDecorator.getImagePath();
        name = currentDecorator.getName();
        description = currentDecorator.getDescription();
    }

    public String saveEditedCategory()
    {
        return Constants.Navigation.CATEGORIES_LINK;
    }

    public List<CategoryDecorator> getCategoryDecorators()
    {
        if (Util.isEmpty(categoryDecorators))
        {
            categoryBOs = getCategoryDAO().getAll();
            categoryDecorators = new ArrayList<CategoryDecorator>();
            for (Category o : categoryBOs)
            {
                categoryDecorators.add(new CategoryDecorator(o.getId(), o.getName(), o.getDescription(), o.getImagePath()));
            }
        }
        return categoryDecorators;
    }

    public void setCategoryDecorators(List<CategoryDecorator> categoryDecorators)
    {
        this.categoryDecorators = categoryDecorators;
    }

    public String getCatId()
    {
        return catId;
    }

    public void setCatId(String catId)
    {
        this.catId = catId;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    public String getImageSrc()
    {
        return imageSrc;
    }

    public void setImageSrc(String imageSrc)
    {
        this.imageSrc = imageSrc;
    }
}
